#include "CTween.h"

Int32 CALLBACKTYPE GetCircularValue(
    /* [in] */ Int32 fromValue,
    /* [in] */ Int32 toValue,
    /* [in] */ Millisecond32 elaspedTime,
    /* [in] */ Millisecond32 durationTime,
    /* [in] */ EaseType easeType)
{
    double dx = (double)(toValue - fromValue);
    double delta = 0;
    double ratio = ((double)elaspedTime) / durationTime;

    switch (easeType) {
    case EaseType_In:
        delta = dx * (1 - sqrt(1 - ratio * ratio));
        break;

    case EaseType_Out:
        ratio -= 1.0;
        delta = dx * sqrt(1 - ratio * ratio);
        break;

    case EaseType_InOut:
        ratio *= 2.0;

        if ((elaspedTime << 1) < durationTime) {
            delta = dx * 0.5 * (1 - sqrt(1 - ratio * ratio));
        }
        else {
            delta = dx * 0.5 * (sqrt(1 - (ratio - 2) * (ratio - 2)) + 1);
        }
        break;

    default:
        assert(0);
    }

    return (Int32)(fromValue + delta);
}
